package com.example.mybatisplus.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.query.SQLQuery;

import java.sql.Types;
import java.util.Collections;

/**
 * @author wangtengfei
 * @description
 * @date 2024/1/17 17:11
 */
public class GeneratorUtils2 {

    public static final String basePath = "D://IdeaProjects/mengshang/dam/dam-oms/src/main/java";

    public static void main(String[] args) {
        String url = "jdbc:mysql://11.28.24.231:3307/dam?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true";
        FastAutoGenerator.create(
                        new DataSourceConfig.Builder(url, "tengfei", "Tengfei123")
                                .schema("dam") //数据库 schema(部分数据库适用)
                                .typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                                    if (typeCode == Types.SMALLINT) {
                                        // 自定义类型转换
                                        return DbColumnType.INTEGER;
                                    }
                                    return typeRegistry.getColumnType(metaInfo);

                                })
                                .databaseQueryClass(SQLQuery.class) //数据库查询方式 默认DefaultQuery.class(通用元数据), SQLQuery.class(SQL查询)
                )
                .globalConfig(builder -> {
                    builder.author("wangtengfei") // 设置作者
                            .commentDate("yyyy-MM-dd")
                            .outputDir(basePath); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.imsxf.dam.oms") // 设置父包名
                            .moduleName("") // 设置父包模块名
                            .entity("entity.po")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .xml("mapper.xml")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, basePath + "/com/imsxf/dam/oms/mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("demo") // 设置需要生成的表名
                            .addTablePrefix("t_", "c_") // 设置过滤表前缀
                            .entityBuilder()
                            .enableLombok()
                            .enableFileOverride();
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
